package com.mybatis.service.shop.impl;

import java.util.List;
import javax.annotation.Resource;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;
import com.github.pagehelper.PageHelper;
import com.mybatis.basic.BaseUtils;
import com.mybatis.basic.entity.AjaxJson;
import com.mybatis.basic.entity.EasyuiList;
import com.mybatis.dao.shop.CourseDao;
import com.mybatis.dao.sys.SysPicDao;
import com.mybatis.entity.Course;
import com.mybatis.entity.SysPic;
import com.mybatis.service.shop.CourseService;


@Service
public class CourseServiceImpl extends BaseUtils implements CourseService{

	private static final Logger log = Logger.getLogger(CourseServiceImpl.class);

	@Resource
	private CourseDao courseDao;

	@Resource
	private SysPicDao sysPicDao;

	@Override
	public AjaxJson addCourse(Course course,int picId) {
		AjaxJson ajaxJson = new AjaxJson();
		try {
			int result = courseDao.addCourse(course);
			if(result!=0){
				SysPic pic = sysPicDao.selectPicById(picId);
				pic.setSourceId(getLastStr(course.getClass().getName())+course.getId());
				sysPicDao.updatePic(pic);
				ajaxJson.setSuccess(true);
				ajaxJson.setMsg("添加成功");
				ajaxJson.setObj(course.getId());
			}else{
				ajaxJson.setSuccess(false);
				ajaxJson.setMsg("添加失败");
			}
		} catch (Exception e) {
			e.printStackTrace();
			ajaxJson.setSuccess(false);
			ajaxJson.setMsg("添加失败，程序出错");
		}
		return ajaxJson;
	}

	@Override
	public AjaxJson selectCourseById(int id) {
		AjaxJson ajaxJson = new AjaxJson();
		try {
			Course course = courseDao.selectCourseById(id);
			if (null!=course) {
				ajaxJson.setSuccess(true);
				ajaxJson.setMsg("查询成功");
				ajaxJson.setObj(course);
			}else{
				ajaxJson.setSuccess(false);
				ajaxJson.setMsg("查询失败，数据不存在");
			}
		} catch (Exception e) {
			e.printStackTrace();
			ajaxJson.setSuccess(false);
			ajaxJson.setMsg("查询失败，程序出错");
		}
		return ajaxJson;
	}

	@Override
	public AjaxJson updateCourse(Course course) {
		AjaxJson ajaxJson = new AjaxJson();
		try {
			int result = courseDao.updateCourse(course);
			if(result!=0){
				ajaxJson.setSuccess(true);
				ajaxJson.setMsg("修改成功");
			}else{
				ajaxJson.setSuccess(false);
				ajaxJson.setMsg("修改失败");
			}
		} catch (Exception e) {
			e.printStackTrace();
			ajaxJson.setSuccess(false);
			ajaxJson.setMsg("修改失败，程序出错");
		}
		return ajaxJson;
	}

	@Override
	public AjaxJson delCourse(int id) {
		AjaxJson ajaxJson = new AjaxJson();
		try {
			int result = courseDao.delCourse(id);
			if(result!=0){
				ajaxJson.setSuccess(true);
				ajaxJson.setMsg("删除成功");
			}else{
				ajaxJson.setSuccess(false);
				ajaxJson.setMsg("删除失败");
			}
		} catch (Exception e) {
			e.printStackTrace();
			ajaxJson.setSuccess(false);
			ajaxJson.setMsg("删除失败，程序出错");
		}
		return ajaxJson;
	}

	@Override
	public EasyuiList<Course> coursePageList(Course course,int pageNum,int pageSize) {
		//AjaxJson ajaxJson = new AjaxJson();
		try {
			
			//获取第1页，10条内容，默认查询总数count
			PageHelper.startPage(1, 10);
			//紧跟着的第一个select方法会被分页
			List<Course> list = courseDao.coursePageList(course);
			EasyuiList<Course> result = new EasyuiList<>(list);
			return result;
			//ajaxJson.setSuccess(true);
			//ajaxJson.setMsg("查询成功");
			//ajaxJson.setObj(result);
		} catch (Exception e) {
			e.printStackTrace();
			//ajaxJson.setSuccess(false);
			//ajaxJson.setMsg("查询失败，程序出错");
		}
		return null;
	}
}
